package com.itheima.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 检查是否登录的过滤器
 */

@WebFilter(urlPatterns = "/*", filterName = "LoginCheckFilter")
@Slf4j
public class LoginCheckFilter implements Filter {
    //    路径通配器,支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;


//        log.info("拦截到请求" + httpServletRequest.getRequestURI());
//        filterChain.doFilter(httpServletRequest, httpServletResponse);

//1.获取本次请求的URI:就是localhost:8080/    的后面参数.
        String requestURI = httpServletRequest.getRequestURI(); //    /backend/page/login/login.html
//        http://localhost:8080/backend/index.html

        log.info("uri是" + requestURI);

//        2.判断本次提交的请求的是否需要处理     定义不需要处理的请求路径
//        想看就看只要进入主页但是不能看到数据
//        数据在/employee/ 开头的地方,之后发起请求.   静态的在/backend/**
        String[] urls = {"/employee/login", "/employee/logout", "/backend/**", "/common/**", "/user/sendMsg",//这是获得验证码
                "/user/login",//这是点击登录
                "/front/page/login.html", "/front/js/**",
                "/front/styles/**",
                "/front/api/**", "/front/fonts/**",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs",
                "/doc.html"
        };
//        只要路径是这几个中的一个就放行.
//        在urls中不需要处理.
        if (check(urls, requestURI) == true) {
            log.info("不需要拦截请求" + requestURI);
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }

//        boolean match = PATH_MATCHER.match("/backend/**","/backend/index.html" );
//        System.out.println("这两个是"+match);
        //需要检查

        Object user = httpServletRequest.getSession().getAttribute("user");
        Object employee = httpServletRequest.getSession().getAttribute("employee");

        if (employee != null) {
            log.info("用户已经登录id为" + employee);
//            long id = Thread.currentThread().getId();
//            log.info("线程"+id);

            BaseContext.setCurrenId((Long) employee);
//            登录了.放行.
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }

//移动端用户.
        if (user != null) {
            log.info("用户已经登录id为" + user);
//            long id = Thread.currentThread().getId();
//            log.info("线程"+id);
            BaseContext.setCurrenId((Long) user);
//            登录了.
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }

//没有登录
        log.info("用户没有登录");
        httpServletResponse.getWriter().write(JSON.toJSONString(R.error("NOLogin")));


    }


    /**
     * 路径匹配器检查
     *
     * @param requestURI
     * @return
     */
    public boolean check(String[] urls, String requestURI) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
//          /backend/index.html
            if (match == true) {
                return true;
            }
        }
        return false;
    }
}
